package trabalho.xml.gui;

import java.net.UnknownHostException;
import java.util.*;

import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import trabalho.xml.dominio.*;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class P2PJobs extends org.eclipse.swt.widgets.Dialog {

	public static Shell dialogShell;
	private static Table TblPeers;
	private Label label1;
	private TableColumn clmTempoFim;
	private Button Job;
	private TableColumn clmTempoInicio;
	private TableColumn clmJobID;
	private Table tarefas;
	private Button addPeer;
	private TableColumn clmStatusPeer;
	private TableColumn clmNomePeer;
	private TableColumn clmJobIDPeer;
	public LinkedList<Peer> listaPeers;
	public static Shell parent;

	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			P2PJobs inst = new P2PJobs(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public P2PJobs(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);

			dialogShell.setLayout(new FormLayout());
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setSize(568, 316);
			{
				FormData tarefasLData = new FormData();
				tarefasLData.left = new FormAttachment(0, 1000, 245);
				tarefasLData.top = new FormAttachment(0, 1000, 36);
				tarefasLData.width = 278;
				tarefasLData.height = 176;
				tarefas = new Table(dialogShell, SWT.NONE);
				tarefas.setLayoutData(tarefasLData);
				tarefas.setHeaderVisible(true);
				tarefas.setLinesVisible(true);
				{
					clmJobID = new TableColumn(tarefas, SWT.NONE);
					clmJobID.setText("Job ID");
					clmJobID.setWidth(60);
				}
				{
					clmTempoInicio = new TableColumn(tarefas, SWT.NONE);
					clmTempoInicio.setText("Inicio");
					clmTempoInicio.setWidth(60);
				}
				{
					clmTempoFim = new TableColumn(tarefas, SWT.NONE);
					clmTempoFim.setText("Fim");
					clmTempoFim.setWidth(60);
				}
			}
			{
				Job = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				FormData JobLData = new FormData();
				JobLData.left = new FormAttachment(0, 1000, 275);
				JobLData.top = new FormAttachment(0, 1000, 241);
				JobLData.width = 76;
				JobLData.height = 25;
				Job.setLayoutData(JobLData);
				Job.setText("Nova Tarefa");
				Job.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						try {
							JobWidgetSelected(evt);
						} catch (UnknownHostException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
					}
				});
			}
			{
				label1 = new Label(dialogShell, SWT.NONE);
				FormData label1LData = new FormData();
				label1LData.left = new FormAttachment(0, 1000, 18);
				label1LData.top = new FormAttachment(0, 1000, 12);
				label1LData.width = 28;
				label1LData.height = 15;
				label1.setLayoutData(label1LData);
				label1.setText("Peers");
			}
			{
				addPeer = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				FormData addPeerLData = new FormData();
				addPeerLData.left = new FormAttachment(0, 1000, 191);
				addPeerLData.top = new FormAttachment(0, 1000, 42);
				addPeerLData.width = 17;
				addPeerLData.height = 25;
				addPeer.setLayoutData(addPeerLData);
				addPeer.setText("+");
				addPeer.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						try {
							addPeerWidgetSelected(evt);
						} catch (UnknownHostException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				FormData PeersLData = new FormData();
				PeersLData.left = new FormAttachment(0, 1000, 12);
				PeersLData.top = new FormAttachment(0, 1000, 36);
				PeersLData.width = 156;
				PeersLData.height = 213;
				TblPeers = new Table(dialogShell, SWT.NONE);
				TblPeers.setLayoutData(PeersLData);
				{
					clmNomePeer = new TableColumn(TblPeers, SWT.NONE);
					clmNomePeer.setText("Nome");
					clmNomePeer.setWidth(60);
				}
				{
					clmStatusPeer = new TableColumn(TblPeers, SWT.NONE);
					clmStatusPeer.setText("Status");
					clmStatusPeer.setWidth(60);
				}
				{
					clmJobIDPeer = new TableColumn(TblPeers, SWT.NONE);
					clmJobIDPeer.setText("Job ID");
					clmJobIDPeer.setWidth(50);
				}
				TblPeers.setHeaderVisible(true);
				TblPeers.setLinesVisible(true);
			}
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			IPSelect primeiraTela = new IPSelect(parent, SWT.NONE);
			primeiraTela.open();
			ControladorPeerJobs.getControlador().setMeuNome(primeiraTela.strNome);
			ControladorPeerJobs.getControlador().setMeuIp(primeiraTela.strIP);
			ControladorPeerJobs.getControlador().setMinhaPorta(
					primeiraTela.strPorta);
			dialogShell.setText(ControladorPeerJobs.getControlador().getMeuNome() + "@" + ControladorPeerJobs.getControlador().meuIP + ":" + ControladorPeerJobs.getControlador().minhaPorta);
			ControladorPeerJobs.getControlador().iniciarConeccao();			
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void atualizaStatus(String _nome, String _status) {
		TableItem listaPeers[] = TblPeers.getItems();
		for (int i = 0; i < TblPeers.getItemCount(); i++) {
			if (listaPeers[i].getText(0).equals(_nome)) {
				listaPeers[i].setText(1, _status);
				return;
			}
		}
		TableItem novo = new TableItem(TblPeers, SWT.NONE);
		novo.setText(0, _nome);
		novo.setText(1, _status);
	}

	private void addPeerWidgetSelected(SelectionEvent evt)
			throws UnknownHostException {
		// TODO add your code for addPeer.widgetSelected

		AddPeer dlgAddPeer = new AddPeer(this.getParent(), SWT.NONE);
		if (dlgAddPeer.open()) {
			Peer novoPeer = new Peer();
			novoPeer.strIP = dlgAddPeer.strIP;
			novoPeer.strNome = dlgAddPeer.strNome;
			novoPeer.strPorta = dlgAddPeer.strPorta;
			novoPeer.strEstado = "Offline";
			novoPeer.dtUltimaResposta = new Date();
			ControladorPeerJobs.getControlador().addNovoPeer(novoPeer);
			TableItem novo = new TableItem(TblPeers, SWT.NONE);
			novo.setText(0, dlgAddPeer.strNome);
			novo.setText(1, novoPeer.strEstado);
		}
	}

	private void JobWidgetSelected(SelectionEvent evt) 
	     throws UnknownHostException {

		// TODO add your code for Job.widgetSelected
		DlgTarefa dlgTarefa = new DlgTarefa(this.getParent(), SWT.NONE);
		if (dlgTarefa.open()) {
			// ControladorPeerJobs.getControlador().
			ArrayList<Integer> m_listaNumero = dlgTarefa.m_listaNumero;
			if(m_listaNumero.size() > 0)
			    ControladorPeerJobs.getControlador().adicionarTarefa(m_listaNumero);
		}
	}

}
